<template>
  <div class="app-container  bg-gray">
    <transition name="fade-transform" mode="out-in">
      <router-view/>
    </transition>
    <el-row v-if="isShow" class="box-1">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div>
              <el-input v-model="searchValue" placeholder="请输入内容" class="search-input" @change="getData" />
              <el-button type="success" class="bg-green" icon="el-icon-search" @click="getData">搜索</el-button>
            </div>
            <el-button v-if="queryMatch(7)" type="success" class="bg-green" icon="el-icon-circle-plus" @click="toModifyRoles('添加角色')">添加角色</el-button>
          </div>
          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
            <el-table-column prop="id" label="ID" min-width="65" header-align="center" align="center" sortable="custom" />
            <el-table-column prop="name" label="角色名称" min-width="160" header-align="center" align="center" />
            <el-table-column prop="description" label="角色概述" min-width="140" header-align="center" align="center" />
            <el-table-column prop="created_at" label="创建时间" min-width="140" header-align="center" align="center" sortable />
            <el-table-column prop="updated_at" label="更新时间" min-width="140" header-align="center" align="center" sortable />
            <el-table-column v-if="(queryMatch(8)||queryMatch(9))" label="操作" min-width="160" header-align="center" align="center">
              <template slot-scope="scope">
                <el-tooltip content="编辑角色" placement="top">
                  <el-button v-if="queryMatch(8)" type="success" size="small" icon="el-icon-edit" circle
                             class="bg-green" @click="toModifyRoles('编辑角色', scope.row)"/>
                </el-tooltip>
                <el-tooltip content="删除角色" placement="top">
                  <el-button v-if="queryMatch(9)" type="danger" size="small" icon="el-icon-delete" circle
                             @click="toDelete(scope.row)"/>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange } from '../../../utils/table'
export default {
  data() {
    return {
      isShow: false, // 当前页面内容是否显示
      searchValue: '',

      // 表格相关参数
      tableMaxHeight: document.documentElement.clientHeight - 280,
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  watch: {
    $route(to) {
      if (to.name === '角色列表') {
        this.getData()
        setTimeout(() => {
          this.isShow = true
        }, 500)
      } else {
        this.isShow = false
      }
    }
  },
  created() {
    if (this.$route.name === '角色列表') {
      this.isShow = true
      this.getData()
    }
  },
  methods: {
    handleSortChange, handleSizeChange, handleCurrentChange,
    /**
     * 对应接口权限列表(/role/list),
     * token  用户凭证  (必填)
     * page_no  当前页码
     * page_size  每页数量
     * sortName  排序字段名
     * sortOrder  排序方式
     * search  关键词
     * return: {
     *   "id": id
     *   "name": 角色名称
     *   "description": 角色概述
     *   "admin_power": 操作权力
     *   "created_at": 创建时间
     *   "updated_at": 更新时间
     * }
     */
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = '/role/list'
      const data = {
        page_no: this.currentPage,
        page_size: this.pageSize,
        sortName: '',
        sortOrder: '',
        search: this.searchValue
      }
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     * 删除角色
     * row: 当前行的数据
     */
    toDelete(row) {
      this.$confirm('此操作将删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'role/delete/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch(e => {
          this.$message.error('删除失败,请刷新或联系管理员!')
        })
      })
    },

    /**
     * 跳转角色修改页面
     * title: 新增/修改
     * row: 当前行的数据,为空时为新增
     */
    toModifyRoles(title, row) {
      this.isShow = false
      const query = { title: title }
      row && (query.id = row.id, query.name = row.name, query.description = row.description)
      this.$router.push({
        path: '/permissions/roles/modifyRole',
        query: query
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .header {
    @include flex-row(center, space-between);
    padding-bottom: 15px;
    .search-input {
      width: 240px;
    }
  }
}
.dialog {
  .el-input {
    width: 80%;
  }
}
</style>
